home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src_ansi / ace / c / memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-05  |  6.3 KB  |  270 lines

  1. /* << ACE >>
  2.  
  3.    -- Amiga BASIC Compiler --
  4.  
  5.    ** Parser: memory functions **
  6.    ** Copyright (C) 1998 David Benn
  7.    ** 
  8.    ** This program is free software; you can redistribute it and/or
  9.    ** modify it under the terms of the GNU General Public License
  10.    ** as published by the Free Software Foundation; either version 2
  11.    ** of the License, or (at your option) any later version.
  12.    **
  13.    ** This program is distributed in the hope that it will be useful,
  14.    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    ** GNU General Public License for more details.
  17.    **
  18.    ** You should have received a copy of the GNU General Public License
  19.    ** along with this program; if not, write to the Free Software
  20.    ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    Author: David J Benn
  23.    Date: 13th,14th June 1993,
  24.    3rd July 1993
  25.  */
  26.  
  27. #include <string.h>
  28. #include "acedef.h"
  29.  
  30. /* locals */
  31. int struct_member_type;        /* for SWAP -> filled by address_of_object() */
  32.  
  33. /* externals */
  34. extern int sym;
  35. extern int lev;
  36. extern SYM *curr_item;
  37. extern char id[MAXIDSIZE];
  38. extern char tempstrname[80];
  39.  
  40. /* -------------- */
  41. /* POKE functions */
  42. /* -------------- */
  43.  
  44. void poke (void)
  45. {
  46.   int addrtype;
  47.  
  48.   /* get address */
  49.   insymbol ();
  50.   addrtype = make_integer (expr ());
  51.   if ((addrtype == longtype) || (addrtype == shorttype))
  52.     {
  53.       /* make sure address is long */
  54.       if (addrtype == shorttype)
  55.     {
  56.       gen ("move.w", "(sp)+", "d0");
  57.       gen ("ext.l", "d0", "  ");
  58.       gen ("move.l", "d0", "-(sp)");
  59.     }
  60.       if (sym == comma)
  61.     {
  62.       /* get value */
  63.       insymbol ();
  64.       make_sure_short (expr ());
  65.       gen ("move.w", "(sp)+", "d0");    /* data to be poked */
  66.       gen ("move.l", "(sp)+", "a0");    /* address */
  67.       gen ("move.b", "d0", "(a0)");        /* poke (a0),d0 */
  68.     }
  69.     }
  70.   else
  71.     _error (4);
  72. }
  73.  
  74. void pokew (void)
  75. {
  76.   int addrtype;
  77.  
  78.   /* get address */
  79.   insymbol ();
  80.   addrtype = make_integer (expr ());
  81.   if ((addrtype == longtype) || (addrtype == shorttype))
  82.     {
  83.       /* make sure address is long */
  84.       if (addrtype == shorttype)
  85.     {
  86.       gen ("move.w", "(sp)+", "d0");
  87.       gen ("ext.l", "d0", "  ");
  88.       gen ("move.l", "d0", "-(sp)");
  89.     }
  90.       if (sym == comma)
  91.     {
  92.       /* get value */
  93.       insymbol ();
  94.       make_sure_short (expr ());
  95.       gen ("move.w", "(sp)+", "d0");    /* data to be poked */
  96.       gen ("move.l", "(sp)+", "a0");    /* address */
  97.       gen ("move.w", "d0", "(a0)");        /* pokew (a0),d0 */
  98.     }
  99.     }
  100.   else
  101.     _error (4);
  102. }
  103.  
  104. void pokel (void)
  105. {
  106.   int addrtype, datatype;
  107.  
  108.   /* get address */
  109.   insymbol ();
  110.   addrtype = make_integer (expr ());
  111.   if ((addrtype == longtype) || (addrtype == shorttype))
  112.     {
  113.       /* make sure address is long */
  114.       if (addrtype == shorttype)
  115.     {
  116.       gen ("move.w", "(sp)+", "d0");
  117.       gen ("ext.l", "d0", "  ");
  118.       gen ("move.l", "d0", "-(sp)");
  119.     }
  120.       if (sym == comma)
  121.     {
  122.       /* get value */
  123.       insymbol ();
  124.       datatype = make_integer (expr ());
  125.       if ((datatype == shorttype) || (datatype == longtype))
  126.         {
  127.           /* coerce data to long */
  128.           if (datatype == shorttype)
  129.         {
  130.           gen ("move.w", "(sp)+", "d0");
  131.           gen ("ext.l", "d0", "  ");
  132.         }
  133.           else
  134.         gen ("move.l", "(sp)+", "d0");    /* data to be poked */
  135.           gen ("move.l", "(sp)+", "a0");    /* address */
  136.           gen ("move.l", "d0", "(a0)");    /* pokel (a0),d0 */
  137.         }
  138.       else
  139.         _error (4);
  140.     }
  141.     }
  142.   else
  143.     _error (4);
  144. }
  145.  
  146. /* ---- */
  147. /* SWAP */
  148. /* ---- */
  149. void get_obj_info (char *objname, int *object, int *objtype)
  150. {
  151. /* get info about a variable, extvar, array or structure for SWAP */
  152.  
  153.   BOOL found = FALSE;
  154.  
  155.   if (exist (objname, variable))
  156.     found = TRUE;
  157.   else if (exist (objname, extvar))
  158.     found = TRUE;
  159.   else if (exist (objname, array))
  160.     found = TRUE;
  161.   else if (exist (objname, structure))
  162.     found = TRUE;
  163.  
  164.   if (found)
  165.     {
  166.       *object = curr_item->object;
  167.       *objtype = curr_item->type;
  168.     }
  169.   else
  170.     {
  171.       *object = undefined;
  172.       *objtype = undefined;
  173.     }
  174. }
  175.  
  176. void swap (void)
  177. {
  178. /* SWAP <object>,<object> */
  179.  
  180.   char first[MAXIDSIZE], second[MAXIDSIZE];
  181.   int typ1, typ2, dataobj1, dataobj2;
  182.  
  183.   insymbol ();
  184.  
  185.   if (sym != ident)
  186.     _error (7);
  187.   else
  188.     {
  189.       strcpy (first, id);
  190.       address_of_object ();
  191.       get_obj_info (first, &dataobj1, &typ1);
  192.  
  193.       /* get_obj_info() won't tell us about structure member type */
  194.       if (dataobj1 == structure)
  195.     typ1 = struct_member_type;
  196.  
  197.       if (dataobj1 != structure && dataobj1 != array)
  198.     insymbol ();
  199.  
  200.       if (sym != comma)
  201.     _error (16);
  202.       else
  203.     {
  204.       insymbol ();
  205.       if (sym != ident)
  206.         _error (7);
  207.       else
  208.         {
  209.           strcpy (second, id);
  210.           address_of_object ();
  211.           get_obj_info (second, &dataobj2, &typ2);
  212.  
  213.           /* get_obj_info() won't tell us about structure member type */
  214.           if (dataobj2 == structure)
  215.         typ2 = struct_member_type;
  216.  
  217.           if (dataobj2 != structure && dataobj2 != array)
  218.         insymbol ();
  219.  
  220.           /* if two objects are of same data type -> swap them */
  221.           if (typ1 != typ2)
  222.         _error (4);
  223.           else
  224.         {
  225.           gen ("movea.l", "(sp)+", "a2");    /* second address */
  226.           gen ("movea.l", "(sp)+", "a1");    /* first address */
  227.  
  228.           if (typ1 == stringtype)
  229.             {
  230.               /* make copies of two addresses */
  231.               gen ("move.l", "a1", "d1");    /* first address */
  232.               gen ("move.l", "a2", "d2");    /* second address */
  233.  
  234.               make_temp_string ();
  235.  
  236.               /* copy first to temp */
  237.               gen ("movea.l", "d1", "a1");    /* source */
  238.               gen ("lea", tempstrname, "a0");    /* dest */
  239.               gen ("jsr", "_strcpy", "  ");    /* temp = first */
  240.  
  241.               /* copy second to first */
  242.               gen ("movea.l", "d2", "a1");    /* source */
  243.               gen ("movea.l", "d1", "a0");    /* dest */
  244.               gen ("jsr", "_strcpy", "  ");    /* first = second */
  245.  
  246.               /* copy temp to second */
  247.               gen ("lea", tempstrname, "a1");    /* source */
  248.               gen ("movea.l", "d2", "a0");    /* dest */
  249.               gen ("jsr", "_strcpy", "  ");    /* second = temp */
  250.  
  251.               enter_XREF ("_strcpy");
  252.             }
  253.           else if (typ1 == shorttype)
  254.             {
  255.               gen ("move.w", "(a1)", "d0");    /* temp = [first] */
  256.               gen ("move.w", "(a2)", "(a1)");    /* [first] = [second] */
  257.               gen ("move.w", "d0", "(a2)");    /* [second] =temp */
  258.             }
  259.           else
  260.             {
  261.               gen ("move.l", "(a1)", "d0");    /* temp = [first] */
  262.               gen ("move.l", "(a2)", "(a1)");    /* [first] = [second] */
  263.               gen ("move.l", "d0", "(a2)");    /* [second] =temp */
  264.             }
  265.         }
  266.         }
  267.     }
  268.     }
  269. }
  270.